package com.zhengdongzulin.zuke.app.main.apartment


import android.annotation.SuppressLint
import android.content.Intent
import android.net.http.SslError
import android.os.Build
import android.view.View
import android.webkit.*
import android.widget.ImageView
import android.widget.TextView
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager.widget.ViewPager
import com.baidu.mapapi.map.*
import com.baidu.mapapi.model.LatLng
import com.lxj.androidktx.core.*
import com.xuexiang.xui.widget.banner.widget.banner.BannerItem
import com.xuexiang.xui.widget.flowlayout.DefaultFlowTagAdapter
import com.zhengdongzulin.zuke.R
import com.zhengdongzulin.zuke.app.data.HouseItemConfig
import com.zhengdongzulin.zuke.app.data.RoomTypeDTO
import com.zhengdongzulin.zuke.app.data.StoreDetails
import com.zhengdongzulin.zuke.base.BaseActivity
import com.zhengdongzulin.zuke.databinding.ZkActivityStoreDetailsBinding
import com.zhengdongzulin.zuke.ext.*

/**
 * @类名称: CLASS
 * @类描述: 门店详情 公寓详情
 * @创建人：Jeff
 * @创建时间：2021/6/9 20:59
 * @备注：
 */
class ZKStoreDetailsActivity : BaseActivity<StoreDetailsVm, ZkActivityStoreDetailsBinding>() {

    override fun getLayoutResId(): Int = R.layout.zk_activity_store_details

    override fun initVM(): StoreDetailsVm = getVM(StoreDetailsVm::class.java)

    override fun initView() {
    }

    @SuppressLint("SetJavaScriptEnabled")
    override fun initData() {
        mViewModel.get_house_item_id(intent.getStringExtra("id"))
    }

    @Suppress("DEPRECATION")
    override fun initListener() {
    }

    override fun startObserve() {
        mViewModel.get_house_item_id.observe(this, Observer {
            it.let {
                var data = it

                when (data.hiItemNum) {
                    "8号楼" -> {
                        mView.ivAddressView.visibility = View.VISIBLE
                        mView.ivAddressView.setImageResource(R.drawable.detail_map_8)
                    }
                    "9号楼" -> {
                        mView.ivAddressView.visibility = View.VISIBLE
                        mView.ivAddressView.setImageResource(R.drawable.detail_map_9)
                    }
                    "10号楼" -> {
                        mView.ivAddressView.visibility = View.VISIBLE
                        mView.ivAddressView.setImageResource(R.drawable.detail_map_10)
                    }
                    "11号楼" -> {
                        mView.ivAddressView.visibility = View.VISIBLE
                        mView.ivAddressView.setImageResource(R.drawable.detail_map_11)
                    }
                    "12号楼" -> {
                        mView.ivAddressView.visibility = View.VISIBLE
                        mView.ivAddressView.setImageResource(R.drawable.detail_map_12)
                    }

                }

                mView.ivAddressView.setOnClickListener {
                    gotoHouseToNavActivity(
                        data.lat.toString(),
                        data.lng.toString(),
                        data.houseItemName,
                        data.hiItemNum
                    )
                }


                var imagesData = mutableListOf<BannerItem>()
                if (!it.houseItemPicList.isNullOrEmpty()) {
                    for (roomPic in it.houseItemPicList) {
                        imagesData.add(BannerItem().setImgUrl(roomPic.url))
                    }
                    mView.tvCount.text = "1/${imagesData.size}"
                    mView.banner.setSource(imagesData)?.startScroll()
                    mView.banner.viewPager.addOnPageChangeListener(object :
                        ViewPager.OnPageChangeListener {
                        override fun onPageScrollStateChanged(state: Int) {

                        }

                        override fun onPageScrolled(
                            position: Int,
                            positionOffset: Float,
                            positionOffsetPixels: Int
                        ) {

                        }

                        override fun onPageSelected(position: Int) {
                            mView.tvCount.text = "${position + 1}/${imagesData.size}"
                        }
                    })
                }
                mView.titleName.text = it.houseItemName
                if (mShareViewModel.apartmentType.value!! == "1") {//人才公寓
                    mView.money.text = ""
                    mView.money.appendStyleSpan("面议")
                    mView.bottomMoney.text = ""
                    mView.bottomMoney.appendStyleSpan("面议")
                    mView.call.setBackgroundResource(R.drawable.apartment_details_bottom_button_bg_en)
                } else {
                    mView.money.text = "￥${it.minMoney}"
                    mView.money.appendSizeSpan("/月起", 0.46f)
                        .colorSpan(
                            "",
                            mView.money.text.length - 3..mView.money.text.length,
                            color(R.color.black_66)
                        )
                        .styleSpan("", 0..mView.money.text.length - 3)
                    mView.bottomMoney.text = "￥${it.minMoney}"
                    mView.bottomMoney.appendSizeSpan("/月起", 0.46f)
                        .colorSpan(
                            "",
                            mView.money.text.length - 3..mView.money.text.length,
                            color(R.color.black_66)
                        )
                        .styleSpan("", 0..mView.money.text.length - 3)
                    mView.call.setOnClickListener {
                        if (checkLogin()) {
                            callPhoneDialog("联系门店", data.phone).show()
                        }
                    }
                }
                mView.typeCount.text = "${it.roomTypeCount}种房型"
                mView.flow.setAdapter(object : DefaultFlowTagAdapter(this) {
                    override fun getLayoutId(): Int {
                        return R.layout.item_apartment_details_flow
                    }
                })
                mView.flow.setItems(it.roomTypeNames)
                mView.addressDesc.text = it.houseItemAddress
                initMap(it)
                if (it.hiItemDesc.isNullOrEmpty()) {
                    mView.introduceDesc.text = "暂无描述"
                } else {
                    mView.introduceDesc.text = it.hiItemDesc
                }

                mView.introduceMore.setOnClickListener {
                    var v = View.inflate(this, R.layout.popwindow_store_introduce_more, null)
                    var dialog = getBottomDialog(v, 0.7)
                    v.findViewById<View>(R.id.iv_close).setOnClickListener { dialog.dismiss() }
                    v.findViewById<View>(R.id.tv_close).setOnClickListener { dialog.dismiss() }
                    v.findViewById<TextView>(R.id.project_desc).text =
                        if (data.hiItemDesc.isNullOrEmpty())
                            "暂无描述"
                        else
                            data.hiItemDesc
                    v.findViewById<TextView>(R.id.poi_desc).text =
                        if (data.hiZhoubianDesc.isNullOrEmpty())
                            "暂无描述"
                        else
                            data.hiZhoubianDesc
                    v.findViewById<TextView>(R.id.traffic_desc).text =
                        if (data.hiTraffic.isNullOrEmpty())
                            "暂无描述"
                        else
                            data.hiTraffic
                    dialog.show()
                }

//                if (introduce_desc.lineCount > 5) {
                mView.introduceMore.visible()
//                } else {
//                    introduce_more.invisible()
//                }
                if (it.houseItemConfig != null && it.houseItemConfig.size > 0) {
                    mView.clConfigure.visible()
                    var list: List<HouseItemConfig>
                    if (it.houseItemConfig.size > 5) {
                        mView.configureMore.visible()
                        list = it.houseItemConfig.subList(0, 5)
                        var list2 = it.houseItemConfig
                        mView.configureMore.setOnClickListener {
                            var v =
                                View.inflate(this, R.layout.popwindow_store_configure_more, null)
                            var dialog = getBottomDialog(v, 0.4)
                            v.findViewById<View>(R.id.iv_close)
                                .setOnClickListener { dialog.dismiss() }
                            v.findViewById<View>(R.id.tv_close)
                                .setOnClickListener { dialog.dismiss() }
                            v.findViewById<TextView>(R.id.tv_empty).text = "房型配置"

                            v.findViewById<RecyclerView>(R.id.recyclerView).vertical(5).bindData(
                                list2,
                                R.layout.item_apartment_details_configure
                            ) { holder, item, position ->
                                holder.loadImage(R.id.img, item.iconUrl)
                                holder.setText(R.id.name, item.configName)
                            }
                            dialog.show()
                        }
                    } else {
                        mView.configureMore.gone()
                        list = it.houseItemConfig!!
                    }
                    //门店配置
                    mView.rvConfigure.vertical(5).bindData(
                        list,
                        R.layout.item_apartment_details_configure
                    ) { holder, item, position ->
                        holder.loadImage(R.id.img, item.iconUrl)
                        holder.setText(R.id.name, item.configName)
                    }
                } else {
                    mView.clConfigure.gone()
                }
                //房型列表
                mView.recyclerView.vertical().bindData(
                    it.roomTypeListDTO,
                    R.layout.item_apartment_details_list
                ) { holder, item, position ->
                    holder.getView<ImageView>(R.id.img)
                        .loadImage(item.roomTypePicUrl, R.mipmap.error_empty_zheng, 5f)
                    holder.setText(R.id.name, item.roomTypeName)
                    holder.setText(
                        R.id.desc,
                        "${item.doorName}\u0020\u0020${item.roomTypeMaxArea}㎡\u0020\u0020${item.orientation}"
                    )
                    var money = holder.getView<TextView>(R.id.money)

                    if (mShareViewModel.apartmentType.value!! == "1") {//人才公寓
                        money.text = ""
                        money.appendStyleSpan("面议")
                        mView.call.setBackgroundResource(R.drawable.apartment_details_bottom_button_bg_en)
                    } else {
                        money.text = "￥${item.houseMinMoney}"
                        money.appendSizeSpan("/月", 0.46f)
                            .colorSpan(
                                "",
                                money.text.length - 2..money.text.length,
                                color(R.color.black_66)
                            )
                            .styleSpan("", 0..money.text.length - 2)
                    }

                }.itemClick { data: List<RoomTypeDTO>, holder, position ->
                    Intent().put("houseItemName", it.houseItemName)
                        .put("id", it.roomTypeListDTO[position].roomTypeId)
                        .start(this@ZKStoreDetailsActivity, ZKHouseDetailsActivity::class.java)
                }
            }
        })
        mViewModel.get_house_item_id.state.observe(this, getObserver(mViewModel.get_house_item_id))
    }

    private fun initMap(roomDetails: StoreDetails) {
        mView.bmapView.apply {
            showScaleControl(false)//显示比例尺控件
            showZoomControls(false)//显示缩放级别
            removeViewAt(1)//隐藏logo
        }.map.apply {
            var llCentre = LatLng(roomDetails.lat, roomDetails.lng)
            val bitmap: BitmapDescriptor = BitmapDescriptorFactory
                .fromResource(com.zhengdongzulin.zuke.R.mipmap.marker)
            val option: OverlayOptions = MarkerOptions()
                .position(llCentre)
                .icon(bitmap)
            addOverlay(option)
            var builder = MapStatus.Builder()
            builder.target(llCentre)//缩放中心点
                .zoom(18f)//缩放级别
            animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()))
        }.uiSettings.apply {
            setAllGesturesEnabled(false)//禁用地图所有手势
        }
    }


    override fun onResume() {
        super.onResume()
        mView.bmapView.onResume()
    }

    override fun onPause() {
        super.onPause()
        mView.bmapView.onPause()
    }

    override fun onDestroy() {
        super.onDestroy()
        mView.bmapView.onDestroy()
    }

}